//
//  CZWelcomeViewController.swift
//  05.08--weibo
//
//  Created by fennyYang on 16/5/15.
//  Copyright © 2016年 a. All rights reserved.
//

import UIKit

import SnapKit
import SDWebImage

class CZWelcomeViewController: UIViewController
{

    override func viewDidLoad()
    {
        super.viewDidLoad()

        prepareUI()

        // 即使头像在本地缓存了图片,刚开始会用占位图片,只有网路请求完成后才会使用SDWebImage设置图片
        // 解决方法,在获取用户信息之前,也使用SDWebImage去设置一下头像的图片
        setIcon()


        // 获取用户信息
        CZUserAccountViewModel.sharedInstance.loadUserInfo
          { () -> () in

               self.setIcon()
          }

     }

     private func setIcon()
     {
          if let avatar_large = CZUserAccountViewModel.sharedInstance.userAccount?.avatar_large
          {
               // 获取到了用户数据，设置头像
               let url = NSURL(string: avatar_large)!

               self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
          }
     }

     override func viewDidAppear(animated: Bool)
     {
          super.viewDidAppear(animated)

          // 在用户看得到界面的时候再调用动画
          moveAnimation()

     }

     // 移动动画
     private func moveAnimation()
     {
          // 弹簧动画
          // usingSpringWithDamping: 弹簧的明显程度 0 - 1
          // initialSpringVelocity: 初始速度
          UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in

               // 要做动画
               // snp_updateConstraints: 更新约束
               self.iconView.snp_updateConstraints(closure:
                    { (make) -> Void in
                        // 更新底部约束
                        make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height - 160))
                    })

               // 根据约束重新设置frame值
               self.view.layoutIfNeeded()
               })
               { (_) -> Void in

                    CZPrint(items: "弹簧动画完成，label渐变动画")

                    UIView.animateWithDuration(0.5, animations:    { () -> Void in

                             self.messageLabel.alpha = 1

                        },
                         completion:{ (_) -> Void in

                                       // 切换到mainViewController
                                       let appDelegate = (UIApplication.sharedApplication().delegate as!AppDelegate)

                                       appDelegate.switchRootViewController(CZMainTabBarController())
                                    })
               }
      }

     private func prepareUI()
     {
          // self.view 添加子控件
          view.addSubview(bkgView)
          view.addSubview(iconView)
          view.addSubview(messageLabel)

          // autolayout
          // 背景
          // bkgView: 要添加约束的view
          // snp_makeConstraints: 要添加约束
          // make: 要添加约束的view
          bkgView.snp_makeConstraints
             { (make) -> Void in

                 //填充父控件
                  make.edges.equalTo(self.view)
             }

          //头像
          iconView.snp_makeConstraints
               { (make) -> Void in

                    // make.centerX:要约束view的centerX属性
                    // equalTo: 参照
                    // sele.view :参照的view
                    // snp_centerX：参照view的哪个属性，参照view的属性需要添加snp_make.centerX.equalTo(self.view.snp_centerX)
                    make.centerX.equalTo(self.view) //当要约束的约束和参照的约束一样的时候，参照的约束可以省略

                    make.bottom.equalTo(self.view).offset(-160)

                    make.size.equalTo(CGSize(width: 85, height: 85))
               }

          // 消息label
          messageLabel.snp_makeConstraints
               { (make) -> Void in

                    make.centerX.equalTo(iconView)

                    make.top.equalTo(iconView.snp_bottom).offset(16)
               }
     }



     //MARK: - 懒加载
     // 背景
     private lazy var bkgView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))

     //头像
     private lazy var iconView: UIImageView =
     {
          let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))

          //圆角
          imageView.layer.cornerRadius = 42.5
          imageView.layer.masksToBounds = true

          return imageView

     }()

     //消息label
     private lazy var messageLabel: UILabel =
     {
          let label = UILabel()

          //设置label
          label.text = "欢迎归来"
          label.textColor = UIColor.blackColor()
          label.font = UIFont.systemFontOfSize(15)
          label.sizeToFit()

          return label

     }()


}



























































